package com.group.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.group.dao.TbAttrNameMapper;
import com.group.dao.TbDeviceAttrMapper;
import com.group.pojo.TbAttrNameExample;
import com.group.pojo.TbDeviceAttr;
import com.group.pojo.TbDeviceAttrExample;
import com.group.pojo.TbDeviceAttrExample.Criteria;
import com.group.service.AttrNameService;
import com.group.service.CommonService;
import com.group.service.DeviceAttrService;
import com.group.service.DeviceService;
import com.group.util.CommonUtil;
@Service
public class DeviceAttrServiceImpl implements DeviceAttrService {
    @Autowired
    private TbDeviceAttrMapper deviceAttrMapper;
    @Autowired
    private DeviceService deviceService;
    @Autowired
    private AttrNameService attrNameService;
    @Autowired
 	private CommonService commonService;
    @Autowired
    private TbAttrNameMapper attrNameMapper;
    //查询所有的设备属性
	@Override
	public List<TbDeviceAttr> getAllDeviceAttrInfo() {
   		TbDeviceAttrExample example = new TbDeviceAttrExample();
   		example.createCriteria().andAttrStatusEqualTo("1");
		return deviceAttrMapper.selectByExample(example);
	}
	//根据设备名称查询设备属性
	@Override
	public List<TbDeviceAttr> getAllDeviceAttrByName(String deviceName) {
		  String deviceId = deviceService.getDeviceIdByName(deviceName);
		  TbDeviceAttrExample example = new TbDeviceAttrExample();
		  example.createCriteria().andDeviceIdEqualTo(deviceId).andAttrStatusEqualTo("1");
		return deviceAttrMapper.selectByExample(example);
	}
	 
	//通过id查询设备属性
	@Override
	public List<TbDeviceAttr> getDeviceAttrById(String id) {
		TbDeviceAttrExample example = new TbDeviceAttrExample();
		example.createCriteria().andIdEqualTo(id);
		return deviceAttrMapper.selectByExample(example);
	}
	//修改设备的属性
	@Override
	public Map<String, String> updateDeviceAttr(TbDeviceAttr deviceAttr) {
		TbDeviceAttr newDeviceAttr = new TbDeviceAttr();//临时对象
		newDeviceAttr = this.getDeviceAttrById(deviceAttr.getId()).get(0);
		newDeviceAttr.setAttrValue(deviceAttr.getAttrValue());
		// 提交到数据库
		int insert = deviceAttrMapper.updateByPrimaryKeySelective(newDeviceAttr);
		Map<String, String> returnMap = CommonUtil.getReturnMap("success", "false");

		if (insert == 1) {
			returnMap.put("success", "true");
		}
		return returnMap;
	}
	//删除设备属性信息
	@Override
	public Map<String, String> deleteDeviceAttrInfo(String[] deviceAttrIds) {
		TbDeviceAttr deviceAttr = new TbDeviceAttr();
		deviceAttr.setAttrStatus("0");
		Map<String, String> map = CommonUtil.getReturnMap("success", "true");
		for (String deviceAttrId : deviceAttrIds) {
			deviceAttr.setId(deviceAttrId);
			int update = deviceAttrMapper.updateByPrimaryKeySelective(deviceAttr);
			if (update != 1) {
				map.put("success", "false");
				return map;
			}
		}
		return map;
	}
	//添加设备属性时的属性验证
	@Override
	public String addDeviceCheckattrName(String attrName,String deviceId) {
		TbAttrNameExample example = new TbAttrNameExample();
		TbDeviceAttrExample example1 = new TbDeviceAttrExample();
		String attrNameId;
		int cunt1;
		attrNameId = attrNameService.getAttrNameIdByName(attrName);
		if(attrNameId == null){
			cunt1 = 999;
		}else{
			example1.createCriteria().andDeviceIdEqualTo(deviceId).andAttrNameIdEqualTo(attrNameId).andAttrStatusEqualTo("1");
			cunt1 = deviceAttrMapper.countByExample(example1);
		}
		example.createCriteria().andAttrNameEqualTo(attrName).andAttrNameStatusEqualTo("1");
		int cunt = attrNameMapper.countByExample(example);
		if(cunt==0 || cunt1 == 999){
			return "false";
		}else if(cunt1 !=0){
			return "false";
		}
		return "true";
	}
	//修改设备属性时的属性验证
	@Override
	public String updateDeviceCheckattrName(String attrName, String deviceId,String id) {
		TbAttrNameExample example = new TbAttrNameExample();
		TbDeviceAttrExample example1 = new TbDeviceAttrExample();
		TbDeviceAttrExample example2 = new TbDeviceAttrExample();
		String attrNameId;
		int cunt;
		int cunt1;
		attrNameId = attrNameService.getAttrNameIdByName(attrName);
		if(attrNameId == null){//判断设备是否已经具备该属性
			cunt1 = 999;
		}else{
			example1.createCriteria().andDeviceIdEqualTo(deviceId).andAttrNameIdEqualTo(attrNameId).andAttrStatusEqualTo("1");
			example2.createCriteria().andIdEqualTo(id).andAttrStatusEqualTo("1");
			List<TbDeviceAttr> list1 = deviceAttrMapper.selectByExample(example1);
			List<TbDeviceAttr> list2 = deviceAttrMapper.selectByExample(example2);
			if(list1.get(0).getAttrNameId().equals(list2.get(0).getAttrNameId())){
				cunt1 = 0;
			}else{
				cunt1 = 1;
			}
		}
		example.createCriteria().andAttrNameEqualTo(attrName).andAttrNameStatusEqualTo("1");
		cunt = attrNameMapper.countByExample(example);
		if(cunt==0 || cunt1 == 999){
			return "false";
		}else if(cunt1 !=0){
			return "false";
		}
		return "true";
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	@Override
	public List<TbDeviceAttr> query(TbDeviceAttr deviceAttr) {
		TbDeviceAttrExample example = new TbDeviceAttrExample();
		setCondition(example.createCriteria(), deviceAttr);
		List<TbDeviceAttr> list = deviceAttrMapper.selectByExample(example);
		return list;
	}
	private void setCondition(Criteria criteria, TbDeviceAttr deviceAttr) {
		String id = deviceAttr.getId();
		if (id != null && !"".equals(id)) {
			criteria.andIdEqualTo(id);
		}
		
		String deviceId = deviceAttr.getDeviceId();
		if (deviceId != null && !"".equals(deviceId)) {
			criteria.andDeviceIdEqualTo(deviceId);
		}
		
		String attrStatus = deviceAttr.getAttrStatus();
		if (attrStatus != null && !"".equals(attrStatus)) {
			criteria.andAttrStatusEqualTo(attrStatus);
		}
	}
	@Override
	public Map<String, String> update(TbDeviceAttr deviceAttr) {
		Map<String, String> returnMap = CommonUtil.getReturnMap("success", "false");
		
		int update = deviceAttrMapper.updateByPrimaryKeySelective(deviceAttr);
		if (update == 1) {
			returnMap.put("success", "true");
		}
		return returnMap;
	}
}
